package framework.base;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import cn.gong.movie.db.AppSQLiteOpenHelper;
import cn.gong.movie.enums.DbTaskType;
import framework.database.async.AsyncData;
import framework.database.callback.DBCallBack;
import framework.database.callback.DBDeal;
import framework.manage.ManageSqliteHelper;
import framework.utils.LogUtils;

public class BaseDb {
	protected static final String INTEGER = "integer";
	protected static final String VARCHAR = "varchar";
	protected static final String PRIMARY_KEY = "primary key";
	protected static final String AUTOINCREMENT = "autoincrement";
	protected static final String ID_PK = INTEGER + " " + PRIMARY_KEY;
	protected static final String ID_PK_AUTO = INTEGER + " " + PRIMARY_KEY + " " + AUTOINCREMENT;

	// 获得数据库
	protected SQLiteDatabase getDb(Context context) {
		return ManageSqliteHelper.getHelper(context).getReadableDatabase();
	}

	// 关闭数据库
	protected void closeDb(SQLiteDatabase db) {
		if (db != null && db.isOpen()) {
			db.close();
		}
	}

	// 关闭游标
	protected void closeCursor(Cursor cursor) {
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
	}

	// 关闭数据库和游标
	protected void closeDbAndCursor(SQLiteDatabase db, Cursor cursor) {
		closeDb(db);
		closeCursor(cursor);
	}

	// cursor相关
	protected String getCursorString(Cursor cursor, String columnName) {
		return cursor.getString(cursor.getColumnIndex(columnName));
	}

	protected int getCursorInt(Cursor cursor, String columnName) {
		return cursor.getInt(cursor.getColumnIndex(columnName));
	}

	protected long getCursorLong(Cursor cursor, String columnName) {
		return cursor.getLong(cursor.getColumnIndex(columnName));
	}

	public void log(String msg) {
		LogUtils.log(msg);
	}

	public void log(Object msg) {
		log(String.valueOf(msg));
	}

	public void isNull(String name, Object obj) {
		log(name + "==null:" + (obj == null));
	}

	public void exception(Exception e) {
		LogUtils.exception(e);
	}

}
